//
// SoapTrace.Net - A soap message monitoring utility. 
//
// Copyright (C) 2005 Matthew Ward
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Matthew Ward (mrward@users.sourceforge.net)

using SoapTrace.Core.Communication;
using System;
using System.Net;
using System.Net.Sockets;

namespace SoapTrace.Tests.Utils
{
	/// <summary>
	/// A mock <see cref="TcpSocket"/>.
	/// </summary>
	public class MockTcpSocket : ISocket
	{
		private ByteArray m_ReadBytes = new ByteArray( );
		
		private IPEndPoint m_RemoteEndPoint 
			= new IPEndPoint( IPAddress.Parse( "200.1.1.1" ), 80 );
		
		private IPEndPoint m_LocalEndPoint 
			= new IPEndPoint( IPAddress.Parse( "127.0.0.1" ), 7000 );

		/// <summary>
		/// Creates a new instance of the 
		/// <see cref="MockTcpSocket"/>.
		/// </summary>
		public MockTcpSocket( )
		{
		}
		
		/// <summary>
		/// Sets the bytes to return when reading.
		/// </summary>
		/// <param name="bytes">A byte array.</param>
		public void SetReadBytes( byte[ ] bytes )
		{
			m_ReadBytes = new ByteArray( );
			m_ReadBytes.Add( bytes, bytes.Length );
		}
		
		/// <summary>
		/// Closes the socket.
		/// </summary>
		public void Close( )
		{
		}
		
		/// <summary>
		/// Establishes a connection to a remote host.
		/// </summary>
		/// <param name="remoteEP">An end point that represents
		/// the remote device.</param>
		public void Connect( EndPoint remoteEP )
		{
		}

		/// <summary>
		/// Determines the status of the Socket.
		/// </summary>
		/// <param name="microSeconds">The time to wait for a response, in 
		/// microseconds.</param>
		/// <param name="mode">One of the <see cref="SelectMode"/> 
		/// values. </param>
		public bool Poll( int microSeconds, SelectMode mode )
		{
			return true;
		}
		
		/// <summary>
		/// Receives data from a bound Socket into a receive buffer.
		/// </summary>
		/// <param name="buffer">An array of type Byte that is the storage 
		/// location for the received data.
		/// </param>
		/// <returns>The number of bytes received.</returns>
		public int Receive( byte[ ] buffer )
		{
			int BytesReceived = 0;
			
			byte[ ] ReadBytes = m_ReadBytes.GetBytes( );
			
			if( ReadBytes.Length <= buffer.Length )
			{
				ReadBytes.CopyTo( buffer, 0 );
				BytesReceived = ReadBytes.Length;
				m_ReadBytes.Clear( );
			}
			else
			{
				Array.Copy( ReadBytes, buffer, buffer.Length );
				m_ReadBytes.ShiftLeft( buffer.Length );
				BytesReceived = buffer.Length;
			}
			
			return BytesReceived;
		}

		/// <summary>
		/// Disables sends and receives on a socket.
		/// </summary>
		/// <param name="how">One of the SocketShutdown values that specifies 
		/// the operation that will no longer be allowed. </param>
		public void Shutdown( SocketShutdown how )
		{
		}

		/// <summary>
		/// Gets the remote endpoint
		/// </summary>
		public EndPoint RemoteEndPoint 
		{ 
			get
			{
				return m_RemoteEndPoint;
			}
		}
		
		/// <summary>
		/// Gets the local endpoint
		/// </summary>
		public EndPoint LocalEndPoint 
		{ 
			get
			{
				return m_LocalEndPoint;
			}
		}		
		
		/// <summary>
		/// Sends data to a connected Socket.
		/// </summary>
		/// <param name="buffer">An array of of type Byte that contains 
		/// the data to be sent. </param>
		public int Send( byte[ ] buffer )
		{
			return buffer.Length;
		}		
	}
}
